const express = require("express");
const router = express.Router();
const adminsDao = require("../dao/AdminsDao");
const dormAdminsDao = require("../dao/DormAdminsDao");
const studentsDao = require("../dao/StudentsDao");
const menusDao = require("../dao/MenusDao");

//html后缀，表示请求的页面
router.get("/logout.do", (req, res) => {
    res.json({
        code: 200,
        msg: "注销成功"
    });
});
//do后缀，表示处理请求
router.post("/login.do", (req, res, next) => {
    //获取提交的数据
    const {password, account, rem, type} = req.body;
    console.log("login.do");
    //数据验证
    if (!account || account.trim().length < 5) {
        return res.json({
            code: 500,
            msg: "账号长度必须大于5位"
        });
    }
    if (!password || password.length < 3) {
        return res.json({
            code: 500,
            msg: "密码长度必须大于3位"
        });
    }
    if (!type) {
        return res.json({
            code: 500,
            msg: "请选择用户类型"
        });
    }
    let result;//登录查询结果
    let msg;//登录用户信息
    if (type == "0") {
        result = adminsDao.login(account, password);
        msg = "欢迎您，系统管理员";
    } else if (type == "1") {
        result = dormAdminsDao.login(account, password);
        msg = "欢迎您，宿舍管理员";
    } else if (type == "2") {
        result = studentsDao.login(account, password);
        msg = "欢迎您，学生";
    } else {
        return res.json({
            code: 500,
            msg: "用户类型错误"
        });
    }
    if (result) {
        result.then(data => {
            console.log("data", data);
            if (!data) {
                res.json({
                    code: 404,
                    msg: "账号或密码错误"
                });
            }
            if (rem == "1") {
                //req.cookies，获取所有cookie
                //在cookie中保存账号和密码
                //res，响应对象
                res.cookie("account", account, {maxAge: 1000 * 60 * 60 * 24 * 7, signed: true});//maxAge单是毫秒
                res.cookie("password", password, {maxAge: 1000 * 60 * 60 * 24 * 7, signed: true});
            } else {
                // res.cookie("account", "");
                // res.cookie("password", "");
                res.clearCookie("account");
                res.clearCookie("password");
            }
            let app = res.app.locals;
            //登录成功重定向到/admins/list.html
            res.json({
                code: 200,
                // 调用 jwt.sign() 生成 JWT 字符串，三个参数分别为：用户信息对象，加密密钥，配置对象
                token: 'Bearer ' + app.jwt.sign({
                    account: data.account,
                    type: type,
                    msg,
                    role: data.role
                }, app.secretKey, {expiresIn: '7days'})
            });
        }).catch(err => next(err));
    } else {
        res.json({
            code: 404,
            msg: "账号或密码错误"
        });
    }
});

router.get("/getInfo.do", (req, res, next) => {
    res.json({
        code: 200,
        data: {
            // expressJWT会将token字符串中的用户信息对象解析出来，并保存在req.user中
            loginUser: req.user
        }
    });
});

module.exports = router;